<?php
declare (strict_types=1);

namespace app\advertiser\service;

use app\admin\model\advertiser\AdvertiserPage;
use app\admin\model\advertiser\AdvertiserTask;
use app\advertiser\validates\AdvertiserTaskValidate;
use app\BaseService;
use think\exception\ValidateException;
use think\response\Json;

/**
 * 推广任务服务层
 */
class AdTaskService extends BaseService
{
    /**
     * 广告位推广物全部列表
     * @param array $params
     * @return Json
     */
    public function adPageAll(array $params): Json
    {
        $where[] = ['status', '=', 1];
        $where[] = ['closed', '=', 0];
        //搜索名称
        if ($params['name']) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        //获取列表
        $data = AdvertiserPage::getListAll($where, 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 推广任务列表
     * @param array $params
     * @param int $type
     * @return Json
     */
    public function list(array $params, int $type): Json
    {
        $where[] = ['ad_user_id', '=', WS_AD_USER_ID];
        $where[] = ['type', '=', $type];
        $where[] = ['closed', '=', 0];
        //搜索任务名称
        if ($params['task_name']) {
            $where[] = ['task_name', 'like', '%' . $params['task_name'] . '%'];
        }
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        if ($type == 3) {
            $data = AdvertiserTask::getPageList($where, $params['page'], $params['limit'], 'id desc');
        } else {
            $data = AdvertiserTask::getWechatList($where, $params['page'], $params['limit'], 'id desc');
        }
        return jsonReturn(1, $data);
    }

    /**
     * 推广任务增加
     * @param array $params
     * @param int $type
     * @return Json
     */
    public function add(array $params, int $type): Json
    {
        //验证数据
        try {
            validate(AdvertiserTaskValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        //处理参数
        $params['ad_user_id'] = WS_AD_USER_ID;
        $params['type'] = $type;
        $params['device_type'] = json_encode(array_map('intval', $params['device_type']));
        $params['location'] = json_encode(array_map('intval', $params['location']));
        $params['create_time'] = time();
        $params['update_time'] = time();
        if ($type == 3) {
            $params['pictures'] = json_encode($params['pictures']);
        }
        //获取排序值
        $Task = new AdvertiserTask();
        $params['orderby'] = AdvertiserTask::setOrderBy($Task, ['type' => $type]);
        //写入推广任务
        $id = $Task::insertGetId($params);
        if ($id) {
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 推广任务编辑
     * @param array $params
     * @param int $type
     * @return Json
     */
    public function edit(array $params, int $type): Json
    {
        //验证数据
        try {
            validate(AdvertiserTaskValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return jsonReturn(0, [], $e->getError());
        }
        //查询当前id是否存在
        $where[] = ['id', '=', $params['id']];
        $where[] = ['ad_user_id', '=', WS_AD_USER_ID];
        $where[] = ['type', '=', $type];
        $where[] = ['closed', '=', 0];
        $Model = AdvertiserTask::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '任务不存在');
        }
        //判断当前状态(只有1已发布和3审核失败才能编辑)
        if (!in_array($Model->status, [1, 3])) {
            return jsonReturn(1, [], '当前状态不能编辑');
        }
        //修改时间
        $params['device_type'] = json_encode(array_map('intval', $params['device_type']));
        $params['location'] = json_encode(array_map('intval', $params['location']));
        $params['update_time'] = time();
        if ($type == 3) {
            $params['pictures'] = json_encode($params['pictures']);
        }
        //编辑
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, [], '编辑成功');
        }
        return jsonReturn(0, [], '编辑失败');
    }

    /**
     * 推广任务删除
     * @param int $id
     * @param int $type
     * @return Json
     */
    public function del(int $id, int $type): Json
    {
        //查询当前id是否存在
        $where[] = ['id', '=', $id];
        $where[] = ['ad_user_id', '=', WS_AD_USER_ID];
        $where[] = ['type', '=', $type];
        $where[] = ['closed', '=', 0];
        $res = AdvertiserTask::findOne($where);
        if (!$res) {
            return jsonReturn(0, [], '任务不存在');
        }
        //删除
        $res = AdvertiserTask::deleteData(['id' => $id]);
        if ($res) {
            return jsonReturn(1, [], '删除成功');
        }
        return jsonReturn(0, [], '删除失败');
    }

    /**
     * 推广任务提交审核
     * @param int $id
     * @param int $type
     * @return Json
     */
    public function review(int $id, int $type): Json
    {
        //查询当前id是否存在
        $where[] = ['id', '=', $id];
        $where[] = ['ad_user_id', '=', WS_AD_USER_ID];
        $where[] = ['type', '=', $type];
        $where[] = ['closed', '=', 0];
        $Model = AdvertiserTask::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '任务不存在');
        }
        //判断当前状态(只有1已发布和3审核失败才能提交审核)
        if (!in_array($Model->status, [1, 3])) {
            return jsonReturn(0, [], '当前状态不能提交审核');
        }
        //修改字段值
        $res = $Model->save(['status' => 2]);
        if ($res) {
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 推广任务上下架
     * @param int $id
     * @param int $status
     * @param int $type
     * @return Json
     */
    public function shelves(int $id, int $status, int $type): Json
    {
        //查询当前id是否存在
        $where[] = ['id', '=', $id];
        $where[] = ['ad_user_id', '=', WS_AD_USER_ID];
        $where[] = ['type', '=', $type];
        $where[] = ['closed', '=', 0];
        $Model = AdvertiserTask::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '任务不存在');
        }
        //判断当前状态(只有4审核通过、5已上架、6已下架才能上下架)
        if (!in_array($Model->status, [4, 5, 6])) {
            return jsonReturn(0, [], '当前状态不能上下架');
        }
        //修改字段值
        $res = $Model->save(['status' => $status]);
        if ($res) {
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }


}
